package com.zhai.test.algorithm.array;

/**
 * @Description: 搜索插入位置
 * @ClassName: MiddleQuery
 * @Author: zhaiyongxin
 * @Date: 2025/3/11 17:50
 * @Version: 1.0
 */
public class SearchInsertRecord {
    public static void main(String[] args) {
        int[] array  = new int[]{1,3,5,6};
        int target = 5;
        int targetIdx = getTargetIdx(array, target);
        System.out.println(targetIdx);
    }

    public static int getTargetIdx(int[] array, int target) {
        int left = 0;
        int right = array.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (array[mid] == target) {
                return mid;
            }else if (array[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return right+1;
    }


}
